package cn.tedu.jxc.service.impl;

import cn.tedu.jxc.ex.ServiceException;
import cn.tedu.jxc.mapper.CustomerMapper;
import cn.tedu.jxc.pojo.dto.CustomerAddNewDTO;
import cn.tedu.jxc.pojo.entity.Customer;
import cn.tedu.jxc.pojo.vo.CustomerListVO;
import cn.tedu.jxc.pojo.vo.CustomerVO;
import cn.tedu.jxc.service.CustomerService;
import cn.tedu.jxc.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {
   @Autowired
   CustomerMapper customerMapper;

    @Override
    public void insert(CustomerAddNewDTO customerAddNewDTO) {
        log.debug("开始处理[添加客户]的业务,参数:{}",customerAddNewDTO);
        int queryResult = customerMapper.countByCustomerName(customerAddNewDTO.getName());
        if (queryResult>0){
            String message = "添加客户失败,该客户名称已存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerAddNewDTO,customer);
        int count = customerMapper.insert(customer);
        if (count!=1){
            String message = "添加客户失败,服务器正忙!请稍后再试......";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理[删除客户]的业务,参数:{}",id);
        int row = customerMapper.deleteById(id);
        if (row!=1){
            String message = "删除客户失败,服务器正忙!请稍后再试......";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
    }

    @Override
    public void updateById(Long id, CustomerAddNewDTO customerAddNewDTO) {
        log.debug("开始处理根据id{}[修改客户]的业务,参数:{}",id,customerAddNewDTO);
        int count = customerMapper.countByNameAndId(id, customerAddNewDTO.getName());
        if (count>0){
            String message = "修改客户详情失败，客户名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        CustomerVO queryResult = customerMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "修改客户详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Customer customer = new Customer();
        BeanUtils.copyProperties(customerAddNewDTO, customer);
        customer.setId(id);

        // 修改数据
        log.debug("即将修改数据：{}", customer);
        int rows = customerMapper.update(customer);
        if (rows != 1) {
            String message = "修改客户详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public CustomerVO getStandardById(Long id) {
        log.debug("开始处理根据id[查询客户数据]的业务,参数:{}",id);
        return customerMapper.getStandardById(id);
    }

    @Override
    public List<CustomerListVO> getStandardByName(String name) {
        log.debug("开始处理根据名称[查询客户数据]的业务,参数:{}",name);
        return customerMapper.getStandardByName(name);
    }

    @Override
    public List<CustomerListVO> list() {
        log.debug("开始处理[查询客户列表]的业务,无参数");
        return customerMapper.list();
    }
}
